package com.jake.rabbit.bpm.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Aspect
@Component
@Slf4j
public class StopWatchAspect {

    private ThreadLocal<StopWatch> stopWatchThreadLocal = new ThreadLocal<>();

    @Pointcut("execution(public java.util.Map<String, Object> com.jake.rabbit.bpm.controller.AttendanceController.getAttendanceRecord(..))")
    public void stopWatch() {
    }

    @Before(value = "stopWatch()")
    public void start() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        stopWatchThreadLocal.set(stopWatch);
    }

    @AfterReturning(pointcut = "stopWatch()")
    public void end() {
        StopWatch stopWatch = stopWatchThreadLocal.get();
        stopWatch.stop();
        double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
        log.info("耗时：{}", totalTimeSeconds);
    }

}
